package com.rede.didiok.admin.restapi.admin;


import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.rede.didiok.admin.annotion.AuthorityVerify.AuthorityVerify;
import com.rede.didiok.admin.annotion.OperationLogger.OperationLogger;
import com.rede.didiok.admin.support.RankMemberSupport;
import com.rede.didiok.common.annotion.AvoidRepeatableCommit.AvoidRepeatableCommit;
import com.rede.didiok.common.constant.global.Constants;
import com.rede.didiok.common.dto.rank.RankMemberDto;
import com.rede.didiok.common.enums.AuditStatusEnum;
import com.rede.didiok.common.enums.ContributeSourceEnum;
import com.rede.didiok.common.enums.OpenStatusEnum;
import com.rede.didiok.common.exception.ThrowableUtils;
import com.rede.didiok.common.feign.RankFeignClient;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.validator.group.Delete;
import com.rede.didiok.common.validator.group.GetList;
import com.rede.didiok.common.validator.group.Insert;
import com.rede.didiok.common.validator.group.Update;

/**
 * 榜单成员表 RestApi
 *
 * @author Rede
 * @date 2023-09-08
 */

@RestController
@RequestMapping("/admin/rankMember")
@Api(value = "榜单成员相关接口", tags = {"榜单成员相关接口"})
@Slf4j
public class RankMemberRestApi {

    @Resource
    private RankMemberSupport rankMemberSupport;

    @Resource
    private RankFeignClient rankFeignClient;



    @AuthorityVerify
    @ApiOperation(value = "获取榜单成员列表", notes = "获取榜单成员列表", response = String.class)
    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody RankMemberDto rankMemberDto, BindingResult result) {

        // 是否管理端查看
        rankMemberDto.setIsBack(Constants.NUM_ONE);
        ThrowableUtils.checkParamArgument(result);
        return ResultUtil.successWithData(rankMemberSupport.getRankMemberPageList(rankMemberDto));
    }

    @AvoidRepeatableCommit
    @AuthorityVerify
    @OperationLogger(value = "增加榜单成员")
    @ApiOperation(value = "增加榜单成员", notes = "增加榜单成员", response = String.class)
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody RankMemberDto rankMemberDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        rankMemberDto.setRankMemberSource(ContributeSourceEnum.ADMIN_PUBLISH);
        rankMemberDto.setOpenLoadingValid(Integer.valueOf(OpenStatusEnum.OPEN));
        // 后台上架，都是审批通过
        rankMemberDto.setAuditStatus(AuditStatusEnum.AGREE);

        return rankMemberSupport.addRankMember(rankMemberDto);
    }

    @AuthorityVerify
    @OperationLogger(value = "本地榜单成员上传")
    @ApiOperation(value = "本地榜单成员上传", notes = "本地榜单成员上传", response = String.class)
    @PostMapping("/uploadLocalRankMember")
    public String uploadLocalRankMember(@RequestBody List<MultipartFile> filedatas) throws IOException {
        return rankMemberSupport.uploadLocalRankMember(filedatas);
    }

    @AuthorityVerify
    @OperationLogger(value = "编辑榜单成员")
    @ApiOperation(value = "编辑榜单成员", notes = "编辑榜单成员", response = String.class)
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody RankMemberDto rankMemberDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return rankMemberSupport.editRankMember(rankMemberDto);
    }

    @AuthorityVerify
    @OperationLogger(value = "修改榜单成员等级")
    @ApiOperation(value = "修改榜单成员等级", notes = "修改榜单成员等级", response = String.class)
    @PostMapping("/updateLevel")
    public String updateLevel(@Validated({Update.class}) @RequestBody RankMemberDto rankMemberDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return rankMemberSupport.updateLevel(rankMemberDto);
    }

    @AuthorityVerify
    @OperationLogger(value = "发布/下架榜单成员")
    @ApiOperation(value = "发布/下架榜单成员", notes = "发布/下架榜单成员", response = String.class)
    @PostMapping("/publish")
    public String publish(@RequestBody RankMemberDto rankMemberDto) {
        rankMemberDto.setRankMemberSource(ContributeSourceEnum.ADMIN_PUBLISH);
        return rankFeignClient.publishByAdmin(rankMemberDto);
    }


    @AuthorityVerify
    @OperationLogger(value = "推荐榜单成员排序调整")
    @ApiOperation(value = "推荐榜单成员排序调整", notes = "推荐榜单成员排序调整", response = String.class)
    @PostMapping("/editBatch")
    public String editBatch(@RequestBody List<RankMemberDto> list) {
        return rankFeignClient.editBatch(list);
    }

    @AuthorityVerify
    @OperationLogger(value = "删除榜单成员")
    @ApiOperation(value = "删除榜单成员", notes = "删除榜单成员", response = String.class)
    @PostMapping("/delete")
    public String delete(@Validated({Delete.class}) @RequestBody RankMemberDto rankMemberDto, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        rankMemberDto.setRankMemberSource(ContributeSourceEnum.ADMIN_PUBLISH);
        return rankFeignClient.deleteRankMemberByAdmin(rankMemberDto);
    }

    @AuthorityVerify
    @OperationLogger(value = "删除选中榜单成员")
    @ApiOperation(value = "删除选中榜单成员", notes = "删除选中榜单成员", response = String.class)
    @PostMapping("/deleteBatch")
    public String deleteBatch(@RequestBody List<RankMemberDto> rankMemberDtoList) {
        return rankFeignClient.deleteBatchRankMember(rankMemberDtoList);
    }

    @AuthorityVerify
    @OperationLogger(value = "审核榜单成员")
    @ApiOperation(value = "审核榜单成员", notes = "审核榜单成员", response = String.class)
    @PostMapping("/audit")
    public String audit(@RequestBody RankMemberDto rankMemberDto) {
        return rankMemberSupport.auditRankMember(rankMemberDto);
    }

    @AuthorityVerify
    @OperationLogger(value = "批量审核榜单成员")
    @ApiOperation(value = "批量审核榜单成员", notes = "批量审核榜单成员", response = String.class)
    @PostMapping("/batchAuditRankMember")
    public String batchAuditBlog(@RequestBody List<RankMemberDto> rankMemberDtoList) {
        return rankMemberSupport.batchAuditRankMember(rankMemberDtoList);
    }

    @AuthorityVerify
    @OperationLogger(value = "榜单成员简介刷数")
    @ApiOperation(value = "榜单成员简介刷新", notes = "榜单成员简介刷数", response = String.class)
    @PostMapping("/flushRankMemberSummary")
    public String flushRankMemberSummary() {
        return rankFeignClient.flushRankMemberSummary();
    }

    @AuthorityVerify
    @OperationLogger(value = "推送选择榜单成员")
    @ApiOperation(value = "推送选择榜单成员", notes = "推送选择榜单成员", response = String.class)
    @PostMapping("/pushBatch")
    public String pushBatch(@RequestBody List<RankMemberDto> rankMemberDtoList) {
        return rankMemberSupport.pushBatch(rankMemberDtoList);
    }

}
